<template>
	<view>
		<view class="form-container">
			<view class="form-item">
				<view class="label require">
					联系人
				</view>
				<input class="sun-input" v-model="groupItem.linkMan" type="text" placeholder="请输入你的姓名" />
			</view>
			<view class="form-item">
				<view class="label require">
					手机
				</view>
				<input class="sun-input" v-model="groupItem.phoneNumber" type="text" placeholder="请输入你的手机" />
			</view>
			<view class="form-item">
				<view class="label">
					意向小区
				</view>
				<input class="sun-input" v-model="groupItem.wantVillage" type="text" placeholder="请输入你期望的小区" />
			</view>
			<view class="form-item">
				<view class="label">
					意向楼层
				</view>
				<input class="sun-input" v-model="groupItem.wantLayer" type="text" placeholder="请输入你期望的楼层" />
			</view>
			<view class="form-item">
				<view class="label">
					意向户型
				</view>
				<input class="sun-input" v-model="groupItem.wantLayout" type="text" placeholder="请输入你期望的户型" />
			</view>
			<view class="btn-box">
				<button class="btn btn-primary" type="default" @click="submit">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				groupItem: {
					wantVillage: '',
					wantLayer: '',
					wantLayout: '',
					linkMan: '',
					phoneNumber: '',
					groupBuyId: null
				}
			};
		},
		onLoad(e) {
			if (e.id) {
				this.groupItem.groupBuyId = e.id;
			}
			this.groupItem.phoneNumber = this.user.phoneNumber;
			this.groupItem.linkMan = this.user.name;
		},
		methods: {
			submit() {
				if (!this.groupItem.groupBuyId) {
					uni.showToast({
						title: '对不起，你参与的团不存在'
					});
					return;
				}
				if (!this.groupItem.linkMan) {
					uni.showToast({
						title: '请输入联系人'
					});
					return;
				}
				if (!this.groupItem.phoneNumber) {
					uni.showToast({
						title: '请输入联系电话'
					});
					return;
				}
				if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.groupItem.phoneNumber)) {
					uni.showToast({
						title: '手机号码格式不正确'
					});
					return;
				}
				this.$http.post('/api/services/app/GroupItem/Create', this.groupItem)
					.then(res => {
						uni.showToast({
							title: '恭喜你，你已参团成功！'
						});
						const channel = this.getOpenerEventChannel();
						channel.emit("joinGroupSuccess");
						uni.navigateBack({
							delta: 1
						})
					});
			}
		},
		computed: {
			user() {
				return this.$store.state.session.user;
			}
		}
	}
</script>

<style lang="scss">
	.form-container {
		padding: 24rpx;
		margin: 16rpx auto;

		.form-item {
			font-size: 14px;

			.label {
				margin: 5px auto;

				&.require::after {
					content: '*';
					color: #FF0000;
				}
			}

			.sun-input {
				font-size: 14px;
				border: 1px solid $uni-border-color;
				padding: 6px;
			}

			.sun-select {
				font-size: 14px;
				border: 1px solid $uni-border-color;
				padding: 6px;
				display: flex;
				flex-wrap: nowrap;
				align-items: center;

				.select-text {
					flex-grow: 1;
					color: #808080;
				}

				.iconfont {
					font-size: 14px;
				}
			}
		}
	}
</style>
